package com.jyuxuan.recruitment.config.shiro;

import com.jyuxuan.recruitment.mapper.EnterpriseLoginMapper;
import com.jyuxuan.recruitment.pojo.EnterpriseLogin;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class EnterpriseRealm extends AuthorizingRealm {

    @Autowired
    EnterpriseLoginMapper enterpriseLoginMapper;

    private final Logger logger = LoggerFactory.getLogger(AdminRealm.class);


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行授权逻辑");
        //给资源进行授权
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //不授权先不写
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException {
        System.out.println("执行认证逻辑"); // 编写shiro判断逻辑，判断用户名和密码
        RoleToken token = (RoleToken) arg0; // 判断用户名
        EnterpriseLogin enterpriseLogin = enterpriseLoginMapper.findByNumber(token.getUsername());
        if (enterpriseLogin == null) { // 该用户不存在
            return null; // shiro底层会抛出 UnKnowAccountException
        }
        return new SimpleAuthenticationInfo(enterpriseLogin, enterpriseLogin.getPassword(), "Enterprise"); // 判断密码
    }


}
